﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace APP.HELPER.Utils.Security
{
    public class MyEncryption
    {
        // Methods
        public string byteArrayToString(byte[] inputArray)
        {
            StringBuilder builder = new StringBuilder("");
            for (int i = 0; i < inputArray.Length; i++)
            {
                builder.Append(inputArray[i].ToString("X2"));
            }
            return builder.ToString();
        }

        public string ByteArrayToString(byte[] arryInput)
        {
            StringBuilder builder = new StringBuilder(arryInput.Length);
            for (int i = 0; i < arryInput.Length; i++)
            {
                builder.Append(arryInput[i].ToString());
            }
            return builder.ToString();
        }

        public static string Decrypt(string toDecrypt, string key, bool useHashing)
        {
            byte[] bytes;
            byte[] buffer3;
            if (toDecrypt == "")
            {
                return "";
            }
            byte[] inputBuffer = Convert.FromBase64String(toDecrypt);
            if (useHashing)
            {
                bytes = new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(key));
            }
            else
            {
                bytes = Encoding.UTF8.GetBytes(key);
            }
            TripleDESCryptoServiceProvider provider2 = new TripleDESCryptoServiceProvider();
            provider2.Key = bytes;
            provider2.Mode = CipherMode.ECB;
            provider2.Padding = PaddingMode.PKCS7;
            ICryptoTransform transform = provider2.CreateDecryptor();
            try
            {
                buffer3 = transform.TransformFinalBlock(inputBuffer, 0, inputBuffer.Length);
            }
            catch
            {
                throw new ArgumentException("File Data is bad.");
            }
            return Encoding.UTF8.GetString(buffer3);
        }

        public string EncodePassword(string originalPassword)
        {
            MD5 md = new MD5CryptoServiceProvider();
            byte[] bytes = Encoding.Default.GetBytes(originalPassword);
            string str = BitConverter.ToString(md.ComputeHash(bytes));
            str.Replace("-", "");
            return str;
        }

        public static string Encrypt(string toEncrypt, string key, bool useHashing)
        {
            byte[] buffer;
            if (toEncrypt == "")
            {
                return "";
            }
            byte[] bytes = Encoding.UTF8.GetBytes(toEncrypt);
            if (useHashing)
            {
                buffer = new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(key));
            }
            else
            {
                buffer = Encoding.UTF8.GetBytes(key);
            }
            TripleDESCryptoServiceProvider provider2 = new TripleDESCryptoServiceProvider();
            provider2.Key = buffer;
            provider2.Mode = CipherMode.ECB;
            provider2.Padding = PaddingMode.PKCS7;
            byte[] inArray = provider2.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length);
            return Convert.ToBase64String(inArray, 0, inArray.Length);
        }

        private string EncryptString(string strToEncrypt)
        {
            byte[] bytes = new UTF8Encoding().GetBytes(strToEncrypt);
            byte[] buffer2 = new MD5CryptoServiceProvider().ComputeHash(bytes);
            string str = "";
            for (int i = 0; i < buffer2.Length; i++)
            {
                str = str + Convert.ToString(buffer2[i], 0x10).PadLeft(2, '0');
            }
            return str.PadLeft(0x20, '0');
        }

        public static string InverseByBase(string st, int MoveBase)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < st.Length; i += MoveBase)
            {
                int num;
                if ((i + MoveBase) > (st.Length - 1))
                {
                    num = st.Length - i;
                }
                else
                {
                    num = MoveBase;
                }
                builder.Append(InverseString(st.Substring(i, num)));
            }
            return builder.ToString();
        }

        public static string InverseString(string st)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = st.Length - 1; i >= 0; i--)
            {
                builder.Append(st[i]);
            }
            return builder.ToString();
        }

        public string MD5Encrypt(string phrase)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] inputArray = new MD5CryptoServiceProvider().ComputeHash(encoding.GetBytes(phrase));
            return this.byteArrayToString(inputArray);
        }

        public string SHA1Encrypt(string phrase)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] inputArray = new SHA1CryptoServiceProvider().ComputeHash(encoding.GetBytes(phrase));
            return this.byteArrayToString(inputArray);
        }

        public string SHA384Encrypt(string phrase)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] inputArray = new SHA384Managed().ComputeHash(encoding.GetBytes(phrase));
            return this.byteArrayToString(inputArray);
        }

        public string SHA512Encrypt(string phrase)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] inputArray = new SHA512Managed().ComputeHash(encoding.GetBytes(phrase));
            return this.byteArrayToString(inputArray);
        }

        public string SHA5Encrypt(string phrase)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] inputArray = new SHA256Managed().ComputeHash(encoding.GetBytes(phrase));
            return this.byteArrayToString(inputArray);
        }
    }
}
